package com.mvp.lt.airlineview.location;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.amap.api.maps.model.LatLng;
import com.mvp.lt.airlineview.App;
import com.mvp.lt.airlineview.utils.LatLngUtils;
import com.mvp.lt.airlineview.utils.ToastUtils;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class LocationGroundService extends Service {
    public static boolean isStarted = false;
    public static final String TAG = "LocationGroundService";
    private LatLng latLng;
    private boolean isRun = false;

    //创建服务时调用
    @Override
    public void onCreate() {
        super.onCreate();
        isStarted = true;
        Log.d(TAG, "onCreate");
        isRun = false;


    }

    //服务执行的操作
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        latLng = intent.getParcelableExtra("latLng");
        Log.d(TAG, "onStartCommand" + latLng.latitude + "" + latLng.longitude);
        startMLocationPostionOperable();
        return super.onStartCommand(intent, flags, startId);
    }

    @SuppressLint("CheckResult")
    public void startMLocationPostionOperable() {
        Observable.interval(0, 2, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        startMLocationPostion();
                    }
                });
    }

    /**
     * 开始模拟位置
     */
    public void startMLocationPostion() {
        try {
            if (latLng != null) {
                // 模拟位置（addTestProvider成功的前提下）
                Map<String, Double> clatlng = LatLngUtils.delta(latLng.latitude, latLng.longitude);
                String providerStr = LocationManager.GPS_PROVIDER;
                Location mockLocation = new Location(providerStr);
                mockLocation.setLatitude(clatlng.get("lat")); // 维度（度）
                mockLocation.setLongitude(clatlng.get("lon")); // 经度（度）
                mockLocation.setAltitude(30); // 高程（米）
                mockLocation.setBearing(180); // 方向（度）
                mockLocation.setSpeed(10); //速度（米/秒）
                mockLocation.setAccuracy(0.1f); // 精度（米）
                mockLocation.setTime(new Date().getTime()); // 本地时间
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
                }
                App.getInstance().getLocationManager().setTestProviderLocation(providerStr, mockLocation);
            } else {
                ToastUtils.showToast("位置获取为空");
            }

        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.showToast("设置失败！");
        }
    }

    //销毁服务时调用
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    //bindService()启动Service时才会调用onBind()方法
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


}
